package com.ethink.platform.controller.warehouse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.order.api.PurchaseOrderFacade;
import com.ethink.order.api.info.PurchaseOrderSaleInfo;
import com.ethink.platform.controller.order.info.ExportInfo;
import com.ethink.platform.manager.ExportManager;
import com.ethink.platform.manager.PublicManager;
import com.ethink.warehouse.api.MateInventoryFacade;
import com.ethink.warehouse.api.info.MateInventoryDetailInfo;
import com.ethink.warehouse.api.request.MateInventoryQueryBySkuRequest;
import com.ethink.warehouse.api.request.MateInventoryQueryRequest;
import com.ethink.warehouse.api.response.MateInventoryQueryBySkuResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 仓库库存 Controller
 *
 * @author default
 * @date 2022-11-08
 */
@Tag(name = "MateInventoryController", description = "仓库库存 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/warehouse-service")
public class MateInventoryController extends BaseController {
    @Autowired
    private MateInventoryFacade mateInventoryFacade;

    @Autowired
    private PurchaseOrderFacade purchaseOrderFacade;

    @Autowired
    private PublicManager publicManager;

    @Autowired
    private ExportManager exportManager;

    @Operation(summary = "分页查询")
    @GetMapping("/mateInventory/query")
    public CommonResponse<List<MateInventoryDetailInfo>> queryMateInventory(MateInventoryQueryRequest request) {
        Page<MateInventoryDetailInfo> result = mateInventoryFacade.queryMateInventory(request);
        CommonResponse<List<MateInventoryDetailInfo>> res = succeedWithPage(result);
        MateInventoryDetailInfo ex = mateInventoryFacade.queryMateInventorySum(request);
        ex.setSumQty(ex.getAvailableQty() + ex.getLockQty() + ex.getOnPassageQty());
        res.setExtension(ex);
        return res;
    }

    @Operation(summary = "批次价格库存导出Excel")
    @GetMapping("/mateInventory/export")
    public CommonResponse<ExportInfo> exportMateInventory(MateInventoryQueryRequest request) {
        return succeedWith(exportManager.exportMateInventory(request));
    }

    @Operation(summary = "批次库存列表查询")
    @GetMapping("/mateInventory/list")
    public CommonResponse<List<MateInventoryDetailInfo>> listMateInventory(@RequestParam("idList") List<Long> idList) {
        // 取出详情单号
        List<MateInventoryDetailInfo> mateInventoryDetailInfoList = mateInventoryFacade.listMateInventoryByIdList(idList);
        // 采购详情单号
        List<String> purchaseDetailOrderNoList = mateInventoryDetailInfoList.stream().map(MateInventoryDetailInfo::getPurchaseDetailOrderNo).collect(Collectors.toList());
        // 远程调用查询采购详情
        List<PurchaseOrderSaleInfo> purchaseOrderSaleInfoList = purchaseOrderFacade.listPurchaseOrderDetailSaleDemandOrderNo(purchaseDetailOrderNoList);
        // 封装数据 Map 采购详情单号 purchaseDetailOrderNo
        Map<String, PurchaseOrderSaleInfo> map = new HashMap<>();
        for (PurchaseOrderSaleInfo purchaseOrderSaleInfo : purchaseOrderSaleInfoList) {
            map.put(purchaseOrderSaleInfo.getDetailOrderNo(), purchaseOrderSaleInfo);
        }
        for (MateInventoryDetailInfo mateInventoryDetailInfo : mateInventoryDetailInfoList) {
            PurchaseOrderSaleInfo purchaseOrderSaleInfo = map.get(mateInventoryDetailInfo.getPurchaseDetailOrderNo());
            if (ObjectUtil.isNotEmpty(purchaseOrderSaleInfo)) {
                mateInventoryDetailInfo.setSalePrice(purchaseOrderSaleInfo.getExceptSalePriceTaxLocal());
                mateInventoryDetailInfo.setGrossProfitRate(publicManager.grossProfitRate(mateInventoryDetailInfo.getSalePrice(), mateInventoryDetailInfo.getCostPrice()));
            }
        }
        return succeedWith(mateInventoryDetailInfoList);
    }

    @Operation(summary = "按商品SKU汇总分页查询")
    @GetMapping("/mateInventory/queryBySku")
    public CommonResponse<List<MateInventoryQueryBySkuResponse>> queryMateInventoryBySku(MateInventoryQueryBySkuRequest request) {
        Page<MateInventoryQueryBySkuResponse> result = mateInventoryFacade.queryMateInventoryBySku(request);
        CommonResponse<List<MateInventoryQueryBySkuResponse>> res = succeedWithPage(result);
        MateInventoryQueryBySkuResponse ex = mateInventoryFacade.queryMateInventoryBySkuSum(request);
        ex.setSumQty(ex.getAvailableQty() + ex.getLockQty() + ex.getOnPassageQty());
        res.setExtension(ex);
        return res;
    }

    @Operation(summary = "库存导出Excel")
    @GetMapping("/mateInventory/exportBySku")
    public CommonResponse<ExportInfo> exportMateInventoryBySku(MateInventoryQueryBySkuRequest request) {
        return succeedWith(exportManager.exportMateInventoryBySku(request));
    }

}